package com.westvalley.syn.service;

import java.util.UUID;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.westvalley.common.kit.SqlsKit;
import com.westvalley.common.kit.StringKit;
import com.westvalley.common.kit.SqlsKit.SqlOperateType;
import com.westvalley.common.result.SqlResult;
import com.westvalley.service.cache.NcClientCache;
import com.westvalley.syn.bean.Result;

/**
 * nc客商
 * @author 莫双
 *
 */
public class NcClientService extends NcService{
	private static final String KEY = "nc_client";
	private NcClientCache ncClientCache = NcClientCache.me();
	
	public NcClientService(){
		refreshCache();
		this.url = prop.getPropValue(KEY);
	}
	
	@Override
	protected Result synMainData(JSONArray jsonArrData) {
		SqlResult sqlResultInsert = SqlResult.me();
		SqlResult sqlResultUpdate = SqlResult.me();
		for(int i=0;i<jsonArrData.size();i++){
			JSONObject ob = jsonArrData.getJSONObject(i);
			if( ob!=null ){
				String pk_cubasdoc = ob.getString("pk_cubasdoc");	//客商主键
				String unitcode = ob.getString("unitcode");	//公司编码
				if( StrKit.notBlank(pk_cubasdoc) && StrKit.notBlank(unitcode) ){
					Record ncRecord = this.ncClientCache.getRecordByCode(pk_cubasdoc+"_"+unitcode);	//缓存中信息
					if(ncRecord==null){
						sqlResultInsert.set(insertData(pk_cubasdoc,ob));
					}else{
						sqlResultUpdate.set(updateData(pk_cubasdoc,ob));
					}
				}else{
					return Result.me(new RuntimeException("数据主键为空！"));
				}
			}else{
				return Result.me(new RuntimeException("数据解析失败！"));
			}
		}
		int[] insertNums = sqlResultInsert.batch(500);
		int[] updateNums = sqlResultUpdate.batch(500);
		return Result.me(jsonArrData.size(),insertNums,updateNums);
	}

	@Override
	protected Result synpOtherData(Result result, JSONArray jsonArrData) {
		SqlResult sqlResultInsert = SqlResult.me();
		Db.update("DELETE FROM WV_NC_CLIENT_BANKS");
		for(int i=0;i<jsonArrData.size();i++){
			JSONObject ob = jsonArrData.getJSONObject(i);
			if(ob!=null){
				JSONArray banks = ob.getJSONArray("banks");
				if(banks!=null){
					String pk_cubasdoc = ob.getString("pk_cubasdoc");	//客商主键
					for(int j=0;j<banks.size();j++){
						JSONObject bankInfo = banks.getJSONObject(j);
						if(bankInfo!=null){
							SqlsKit sqlsKit = SqlsKit.me();
							sqlsKit.clear();
							sqlsKit.setTableName("WV_NC_CLIENT_BANKS");
							sqlsKit.addData("id", UUID.randomUUID().toString());
							sqlsKit.addData("pk_cubasdoc", pk_cubasdoc);
							sqlsKit.addData("accountcode", StringKit.null2Str(bankInfo.getString("accountcode")));
							sqlsKit.addData("accountname", StringKit.null2Str(bankInfo.getString("accountname")));
							sqlsKit.addData("acctype", StringKit.null2Str(bankInfo.getString("acctype")));
							sqlsKit.setOperateType(SqlOperateType.INSERT);
							sqlResultInsert.set(sqlsKit);
						}
					}
				}
			}
		}
		sqlResultInsert.batch(500);
		return result;
	}

	@Override
	protected void refreshCache() {
		this.ncClientCache.reload();
	}
	
	/**
	 * 新增数据
	 * @param pk
	 * @param ob
	 * @return
	 */
	private SqlsKit insertData(String pk,JSONObject ob){
		SqlsKit sqlsKit = SqlsKit.me();
		sqlsKit.clear();
		sqlsKit.setTableName("WV_NC_CLIENT");
		sqlsKit.addData("id", UUID.randomUUID().toString());
		sqlsKit.addData("pk_cubasdoc", StringKit.null2Str(ob.getString("pk_cubasdoc"))+"_"+StringKit.null2Str(ob.getString("unitcode")));
		sqlsKit.addData("custcode", StringKit.null2Str(ob.getString("custcode")));
		sqlsKit.addData("custname", StringKit.null2Str(ob.getString("custname")));
		sqlsKit.addData("custshortname", StringKit.null2Str(ob.getString("custshortname")));
		sqlsKit.addData("unitcode", StringKit.null2Str(ob.getString("unitcode")));
		sqlsKit.setOperateType(SqlOperateType.INSERT);
		return sqlsKit;
	}
	
	/**
	 * 更新数据
	 * @param pk
	 * @param ob
	 * @return
	 */
	private SqlsKit updateData(String pk,JSONObject ob){
		SqlsKit sqlsKit = SqlsKit.me();
		sqlsKit.clear();
		sqlsKit.setTableName("WV_NC_CLIENT");
		sqlsKit.addData("custcode", StringKit.null2Str(ob.getString("custcode")));
		sqlsKit.addData("custname", StringKit.null2Str(ob.getString("custname")));
		sqlsKit.addData("custshortname", StringKit.null2Str(ob.getString("custshortname")));
		sqlsKit.addData("unitcode", StringKit.null2Str(ob.getString("unitcode")));
		sqlsKit.setWhere("pk_cubasdoc", StringKit.null2Str(ob.getString("pk_cubasdoc"))+"_"+StringKit.null2Str(ob.getString("unitcode")));
		sqlsKit.setOperateType(SqlOperateType.UPDATE);
		return sqlsKit;
	}

}